'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 1_study_chat_graph.py
* @Time: 2025/11/4
* @All Rights Reserve By Brtc
'''
from typing import TypedDict, Annotated

import dotenv
from langchain_openai import ChatOpenAI
from langgraph.constants import START,END
from langgraph.graph import add_messages, StateGraph

dotenv.load_dotenv()

class ChatBotGraphState(TypedDict):
    """聊天消息就是状态, 消息是一个字典, add_messages 是一个归纳函数"""
    messages:Annotated[list, add_messages]
    node_name:str

llm = ChatOpenAI(model = "gpt-4o-mini")

def chat_bot(state:ChatBotGraphState)->ChatBotGraphState:
    """聊天机器人函数"""
    ai_messsage = llm.invoke(state["messages"])
    return {"messages":[ai_messsage], "node_name":"chat_bot"}

# 第一步定义状态
graph_builder = StateGraph(ChatBotGraphState)
# 第二步骤添加节点
graph_builder.add_node("chat_bot", chat_bot)
# 第三步用边 将节点链接起来 start ----------> chatbot -----------> end
graph_builder.add_edge(START, "chat_bot")
graph_builder.add_edge("chat_bot", END)
# 第四步编译图
graph_app = graph_builder.compile()
#第五步运行图程序
# 传递的 也是一个状态
print(graph_app.invoke({"messages":[("human", "你好你是？")], "node_name":"human_input"}))